﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ReadWriteStudentsInFile
{
    class Program
    {
        static void Main(string[] args)
        {
            string choice = "N";
            string finalChoice = "N";
            List<Student> students = new List<Student>();
            string studentsFileInfoPath = @"C:\Users\anton\Documents\C#\Inteducation\aisaiev\ReadWriteStudentsInFile\ReadWriteStudentsInFile\StudentsInfo.txt";
            do
            {
                Console.Clear();
                Console.WriteLine("1 - Write students info to file");
                Console.WriteLine("2 - Read students info from file to list");
                Console.Write("Select: ");
                int select = Int32.Parse(Console.ReadLine());
                switch (select)
                {
                    case 1:
                        do
                        {
                            Console.Clear();
                            Student student = new Student();
                            student.AddStudentInfo();
                            students.Add(student);
                            Console.Write("Continue students adding? Y/N: ");
                            choice = Console.ReadLine().ToUpper();
                        }
                        while (choice == "Y");

                        foreach (var student in students)
                        {
                            File.AppendAllText(studentsFileInfoPath, student.id + Environment.NewLine);
                            File.AppendAllText(studentsFileInfoPath, student.firstName + Environment.NewLine);
                            File.AppendAllText(studentsFileInfoPath, student.lastName + Environment.NewLine);
                            File.AppendAllText(studentsFileInfoPath, Environment.NewLine);
                        }
                        students.Clear();
                        break;
                    case 2:
                        Console.Clear();
                        string[] studentsFileInfo = File.ReadAllLines(studentsFileInfoPath);
                        for(int i = 0; i < studentsFileInfo.Length; i++)
                        {
                            students.Add(new Student(studentsFileInfo[i], studentsFileInfo[i + 1], studentsFileInfo[i + 2]));
                            i = i + 3;
                        }

                        foreach (var student in students)
                        {
                            Console.WriteLine(student.id);
                            Console.WriteLine(student.firstName);
                            Console.WriteLine(student.lastName);
                        }
                        break;
                }
                Console.Write("Return to main menu? Y/N: ");
                finalChoice = Console.ReadLine().ToUpper();
            }
            while (finalChoice == "Y");
        }
    }
}
